package mall.controller.admin;

import com.google.gson.Gson;
import mall.model.Admin;
import mall.model.Result;
import mall.model.bo.AdminAddAdminBO;
import mall.model.bo.AdminLoginBO;
import mall.model.bo.AdminSearchBO;
import mall.model.vo.AdminLoginVO;
import mall.service.AdminService;
import mall.service.AdminServiceImpl;
import mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-27 15:52
 *
 * */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    private Gson gson = new Gson();
    private AdminService adminService = new AdminServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("login".equals(action)) {
            login(request, response);
        } else if ("getSearchAdmins".equals(action)) {
            getSearchAdmins(request, response);
        } else if ("addAdminss".equals(action)) {
            addAdmins(request, response);
        } else if ("updateAdminss".equals(action)) {
            updateAdmin(request, response);
        }


    }

    /*
    * 修改管理员信息
    * 1. 浏览器向8084发送一个请求，请求体中携带参数{id: , nickname: "", email: "", pwd: ""}
    * 2. 校验校验邮箱，昵称和密码格式是否正确
    * 3. 若不正确，直接返回
    * 4. 若正确，根据id更新数据库表admin，并作出相应{"code":0}
    * */
    private void updateAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        boolean result = adminService.updateAdmin(admin);
        //用户信息正则不匹配
        if (!result) {
            return;
        }
        //修改成功
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 添加管理员账户
     * 1. 浏览器向8084发送一个请求，请求体中携带参数{nickname: "", email: "", pwd: ""}(json字符串)
     * 2. 校验邮箱，昵称和密码格式是否正确
     * 3. 若不正确，直接返回
     * 3. 若正确，检查邮箱是否已存在与数据库表admin中
     *    a. 存在，直接作出响应 {"code":10000,"message":"该账号不允许重复使用"}
     *    b. 不存在，将参数信息插入数据库表admin，并作出响应 {"code":0}
     * */

    /*
    * 账号正则未通过：(必须qq邮箱 qq号5-10位)
    * 昵称正则未通过：(只能包括数字字母组合，长度为4-15位)
    * 密码正则诶通过：(最少8位，最多16位，至少一个大写字母，1个小写字母，1个数字和1个特殊字符)
    * 账号不允许重复：{"code":10000,"message":"该账号不允许重复使用"}
    * 添加成功：{"code":0}
    * */
    private void addAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminAddAdminBO addAdminBO = gson.fromJson(requestBody, AdminAddAdminBO.class);
        Map<String, Object> map = adminService.addAdmin(addAdminBO);
        //用户信息正则不匹配
        if (map.isEmpty()) {
            return;
        }
        //账号重复
        if (!(Boolean) map.get("flag")) {
            response.getWriter().println(gson.toJson(Result.error((String) map.get("content"))));
            return;
        }
        //添加成功
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 查询admin管理员账户信息
     * 1. 浏览器向8084发送一个请求，请求体中携带用户昵称参数{nickname: "", email: ""}(json字符串)
     * 2. 查询数据库，查询满足条件的数据
     * 3. 作出响应
     * */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求体(json ——> 对象集合)
        String requestBody = HttpUtils.getRequestBody(request);
        AdminSearchBO searchBO = gson.fromJson(requestBody, AdminSearchBO.class);
        //获取满足条件的数据
        List<Admin> adminList = adminService.getSearchAdmins(searchBO);
        response.getWriter().println(gson.toJson(Result.ok(adminList)));

    }

    /*
    * 管理员登录具体业务逻辑：
    * 1. 浏览器向8084发送一个请求，请求中携带了y用户名密码参数(json字符串)
    * 2. 查询数据库，校验当前用户名密码是否正确
    * 3. 根据查询结果，返回不同的响应
    * */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求体
        String requestBody = HttpUtils.getRequestBody(request);
        AdminLoginBO loginBO = gson.fromJson(requestBody, AdminLoginBO.class);
        //System.out.println(loginBO);
        Admin login = adminService.login(loginBO);
        if (login != null) {
            request.getSession().setAttribute("admin", login);
            AdminLoginVO loginVO = new AdminLoginVO();
            loginVO.setToken(login.getNickname());
            loginVO.setName(login.getNickname());
            response.getWriter().println(gson.toJson(Result.ok(loginVO)));
        } else {
            response.getWriter().print(gson.toJson(Result.error("，账号与密码不匹配")));
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("allAdmins".equals(action)) {
            allAdmins(request, response);
        } else if ("deleteAdmins".equals(action)) {
            deleteAdmin(request, response);
        } else if ("getAdminsInfo".equals(action)) {
            getAdminInfo(request, response);
        }
    }

    private void getAdminInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Admin admin = adminService.getAdminsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(admin)));
    }

    private void deleteAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        adminService.deleteAdmin(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /*
    * 显示所有admin账户信息
    * 1. 查询数据库，返回所有账户信息数据
    * 2. 做出相应
    * */
    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> adminList = adminService.allAdmins();
        Result result = new Result();
        result.setCode(0);
        result.setData(adminList);
        response.getWriter().println(gson.toJson(result));
    }

}
